import mineflayer from "mineflayer";
import { getBiomeName } from "./getWorldData.js";

// 获取背包物品列表
function _getInventoryPrompt(bot: mineflayer.Bot): string {
  const sum: { [key: string]: number } = {};
  const helmet: string = bot.inventory.slots[5]?.name ?? "空";
  const chestplate: string = bot.inventory.slots[6]?.name ?? "空";
  const leggings: string = bot.inventory.slots[7]?.name ?? "空";
  const boots: string = bot.inventory.slots[8]?.name ?? "空";
  let prompt = "## 物品栏\n";
  for (let i: number = 0; i < bot.inventory.slots.length; ++i) {
    const item = bot.inventory.slots[i];
    if (!item || (i >= 5 && i <= 8)) continue;
    const key = `[${item.name}]`;
    sum[key] ? (sum[key] += item.count) : (sum[key] = item.count);
  }
  for (const key in sum) {
    prompt += `${key}*${sum[key]}\n`;
  }
  prompt += `### 装备栏\n头盔:${helmet}\n胸甲:${chestplate}\n护腿:${leggings}\n靴子:${boots}\n`;
  return prompt;
}

// 月相表
const Phases: Array<string> = [
  "满月",
  "亏凸月",
  "下弦月",
  "残月",
  "新月",
  "娥眉月",
  "上弦月",
  "盈凸月",
];
// 维度表
const DimensionTable = {
  the_nether: "下界",
  overworld: "主世界",
  the_end: "末地",
};
// 获取环境信息
function _getEnvironmentPrompt(bot: mineflayer.Bot): string {
  const weather: string = bot.isRaining ? (bot.thunderState ? "雷雨" : "雨") : "晴";
  const time: number = bot.time.timeOfDay;
  const dimension: string = DimensionTable[bot.game.dimension];
  const biome: string = getBiomeName(bot);
  let timeDesc = "白天";
  if (time > 12000)
    timeDesc =
      time <= 13800 ? "日落" : time <= 22200 ? `晚上 月相:${Phases[bot.time.moonPhase]}` : "黎明";

  return `## 环境信息\n天气:${weather}\n时间:${timeDesc}\n维度:${dimension}\n生物群系:${biome}\n`;
}

// 游戏模式表
const Game_mode = {
  creative: "创造模式",
  survival: "生存模式",
  spectator: "观察者模式",
  adventure: "冒险模式",
};
// 游戏难度表
const Game_difficulty = {
  peaceful: "和平",
  easy: "简单",
  normal: "普通",
  hard: "困难",
};

// 获取自身信息
function _getSelfInfo(bot: mineflayer.Bot): string {
  let prompt = "## 自身状态\n";
  const name = bot.username;
  const health = bot.health;
  const food = bot.food;
  const saturation = bot.foodSaturation;
  const oxygen = bot.oxygenLevel === undefined ? null : bot.oxygenLevel;
  const level = bot.experience.level;
  const difficulty = Game_difficulty[bot.game.difficulty];
  const gameMode = Game_mode[bot.game.gameMode];
  const mcVersion = bot.version;

  prompt += `游戏ID:${name}\n游戏版本:${mcVersion}\n当前血量:${health}\n饱食度:${food}\n饱和度:${saturation}\n等级:${level}\n游戏模式:${gameMode}\n`;
  prompt += `当前坐标: X:${bot.entity.position.x.toFixed(2)} `;
  prompt += `Y:${bot.entity.position.y.toFixed(2)} `;
  prompt += `Z:${bot.entity.position.z.toFixed(2)}\n`;
  if (bot.isSleeping) prompt += "已躺在床上\n";
  if (difficulty !== undefined) prompt += `游戏难度:${difficulty}\n`;
  if (oxygen !== null) prompt += `氧气值:${oxygen}\n`;
  return prompt;
}

// 获取当前状态信息
export function getStatePrompt(bot: mineflayer.Bot) {
  let prompt =
    "# **特殊事件-游戏模式**\n**你现在正在玩Minecraft，注意绝对不要编造没有发生或者不存在的事情，你只能参考上下文输出**。\n";
  prompt += _getSelfInfo(bot);
  prompt += _getEnvironmentPrompt(bot);
  prompt += _getInventoryPrompt(bot);
  return prompt;
}
